<template>
	<om-addpage :menuName="menuName" :busCode="busCode" @formSubmit="formSubmit">
		<view v-show="false">
			<input v-model="formdata.zposId" name="zposId" />
			<input v-model="formdata.zposName" name="zposName" />
			<input v-model="formdata.adjustCode" name="adjustCode" />
			<input v-model="formdata.adjustName" name="adjustName" />
			<input v-model="formdata.cateIds" name="cateIds" />
			<input v-model="formdata.cateName" name="cateName" />
			<input v-model="formdata.userId" name="userId"/>
			<input v-model="formdata.remark" name="remark" />
		</view>
		<uni-list title="社保信息">
			<uni-list-item :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="制表岗位" required></om-label></view>
					<view class="tui-right tui-col-8">
						<picker mode="selector" name="zposIndex" @change="zposFunc" :range="zposList" :value="zposIndex" range-key="posname">
							<view>{{ zposList.length && ~zposIndex ? zposList[zposIndex].posname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="姓名"></om-label></view>
					<view class="tui-right tui-col-8">{{ userName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="入职日期"></om-label></view>
					<view class="tui-right  tui-col-8">{{ date }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="计酬岗位"></om-label></view>
					<view class="tui-right  tui-col-8">{{ salaryPosName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="所属机构"></om-label></view>
					<view class="tui-right  tui-col-8">{{ instName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="调整类别" required></om-label></view>
					<view class="tui-right tui-col-8">
						<picker mode="selector" name="adjustIndex" @change="adjustFunc" :range="adjustList" :value="adjustIndex" range-key="dicname">
							<view>{{ adjustList.length && ~adjustIndex ? adjustList[adjustIndex].dicname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item v-show="!ishidden"  @click="showCate" :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="社保类别" required></om-label></view>
					<view class="tui-col-8">
						<view v-if="selectCateList.length" style="display: flex; flex-flow:row-reverse wrap">
							<label v-for="(item, index) in selectCateList" :key="index">
								<text v-if="index !== 0">{{ item.dicname }},</text>
								<text v-else>{{ item.dicname }}</text>
							</label>
						</view>
						<view class="tui-right" v-else>请选择</view>
						<ot-tree ref="ottree" :range="cateList" rangeKey="dicname" multiple @confirm="cateConfirm"></ot-tree>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="说明" required></om-label></view>
					<view class="tui-left tui-col-8"><textarea :auto-height="true" class="om-textarea" name="remark" v-model="formdata.remark" placeholder="请输入..." /></view>
				</view>
			</uni-list-item>
		</uni-list>
	</om-addpage>
</template>

<script>
import dateTimePicker from '@/static/js/dateTimePicker.js';
import {loadZpos} from '../../../static/js/formOld.js';
import { ignoreHtml} from '@/static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			formdata: {
				zposId: '',
				zposName: '',
				adjustCode: '',
				adjustName: '',
				cateIds: '',
				cateName: '',
				userId:'',
				remark:''
			},
			zposIndex: 0,
			zposList: [],
			date: '',
			salaryPosName: '',
			instName: '',
			adjustIndex: 0,
			adjustList: [],
			DICT_SOCIAL_SECURITY: '',
			DICT_ADJUST_TYPE6_1: '',
			DICT_ADJUST_TYPE6_2: '',
			DICT_ADJUST_TYPE6_3: '',
			DICT_ADJUST_TYPE6_4: '',
			ishidden: true,
			selectCateList: [],
			cateList: [],
			menuName: '',
			id: '',
			busCode: ''
		};
	},
	computed: mapState(useLoginUserStore, ['userName', 'userId']),
	methods: {
		/**
		 * 制表岗位
		 */
		zposFunc(e) {
			let _that = this;
			_that.zposIndex = e.target.value;
			_that.formdata.zposId = this.zposList[e.detail.value].id;
			_that.formdata.zposName = this.zposList[e.detail.value].posname;
			_that.afterZposFunc();
		},
		/**
		 * 加载调整类别
		 */
		loadAdjustFunc() {
			let _that = this;
			_that.om
				.request('wxSocialSecurity/getAdjustList', {
					userId: _that.userId
				})
				.then(res => {
					_that.adjustList = res;
					this.setAdjustValue()
				});
		},
		adjustFunc(e) {
			this.adjustIndex = e.target.value;
			this.setAdjustValue()
		},
		setAdjustValue(){
			let code = this.adjustList[this.adjustIndex].code;
			if (code == this.DICT_ADJUST_TYPE6_2 || code == this.DICT_ADJUST_TYPE6_3) {
				this.ishidden = true;
			} else {
				this.ishidden = false;
				this.cateConfirm(this.cateList)
			}
			this.formdata.adjustCode = code;
			this.formdata.adjustName = this.adjustList[this.adjustIndex].dicname;
		},
		showCate() {
			this.$refs.ottree._show();
		},
		cateConfirm(selectList) {
			this.selectCateList = selectList;
			this.formdata.cateIds = selectList.map(e => e.id);
			this.formdata.cateName = selectList.map(e => e.dicname);
		},

		/**
		 * 日期选择
		 */
		bindDateChange(e) {
			this.adjustProbationDate = e.detail.value;
		},

		formSubmit: function(formdata) {
			let _that = this;
			if(!formdata || Object.keys(formdata).length==0){
				formdata = _that.formdata
			}
			if (!formdata.zposId || !formdata.adjustCode || !formdata.remark) {
				_that.om.toast('必输项未输入完成，请继续编辑');
				return;
			}
			if (formdata.adjustCode == _that.DICT_ADJUST_TYPE6_1 || formdata.adjustCode == _that.DICT_ADJUST_TYPE6_4) {
				if (!formdata.cateIds || !formdata.cateName) {
					_that.om.toast('必输项未输入完成，请继续编辑');
					return;
				}
			}

			_that.om.formSave('wxSocialSecurity/save', formdata, _that);
		},
		initLoad() {
			let _that = this;
			loadZpos(_that, 'afterZposFunc','您在OM系统中没有任职岗位，不能申请社保！');
			//加载社保类别
			_that.om
				.request(
					'tsysdict/getListByCode',
					{
						code: _that.DICT_SOCIAL_SECURITY
					},
					'post'
				)
				.then(res => {
					_that.cateList = res.map(function(e) {
						e.selected = true;
						return e;
					});
				});
		},
		/**
		 * 选择岗位后
		 */
		afterZposFunc() {
			let _that = this;
			_that.om
				.request('wxSocialSecurity/afterZpos', {
					zposId: _that.zposList[_that.zposIndex].id
				})
				.then(res => {
					if (res.succeed) {
						let data = res.value;
						if (data.tip) {
							_that.om.toast(ignoreHtml(data.tip));
							_that.zposIndex = -1;
							return;
						}
						if (data.user) {
							_that.date = data.user.entryDate;
							_that.salaryPosName = data.user.salaryPosName;
							_that.instName = data.user.instName;
							//加载调整类别
							_that.loadAdjustFunc();
						}
					}
				});
		}
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '社保';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		_that.busCode = options.code;
		_that.formdata.userId = _that.userId
		_that.om.request('wxSocialSecurity/getConst').then(res => {
			if (res.succeed) {
				_that.busCode = res.value.busCode || 'HRS1301';
				_that.DICT_SOCIAL_SECURITY = res.value.DICT_SOCIAL_SECURITY; //社保类别
				_that.DICT_ADJUST_TYPE6_1 = res.value.DICT_ADJUST_TYPE6_1; //参保
				_that.DICT_ADJUST_TYPE6_2 = res.value.DICT_ADJUST_TYPE6_2; //调整金额
				_that.DICT_ADJUST_TYPE6_3 = res.value.DICT_ADJUST_TYPE6_3; //退保
				_that.DICT_ADJUST_TYPE6_4 = res.value.DICT_ADJUST_TYPE6_4; //调整参保项目
				_that.initLoad();
			}
		});
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.saveFlag = false;
	}
};
</script>

<style></style>
